﻿using Common.Type;
using System;
using System.Configuration;
using System.Linq;

namespace DataCenter.Configuration
{
    public static class SystemConfiguration
    {
        private static int defaultPageSize = 0;
        public static int DefaultPageSize
        {
            get
            {
                if (defaultPageSize > 0) return defaultPageSize;
                int.TryParse(ConfigurationManager.AppSettings["DefaultPageSize"], out defaultPageSize);
                if (defaultPageSize <= 0) defaultPageSize = 20;
                return defaultPageSize;
            }
        }


        private static string connectionString;
        public static string ConnectionString
        {
            get
            {
                if (!string.IsNullOrWhiteSpace(connectionString)) return connectionString;
                connectionString = ConfigurationManager.AppSettings["ConnectionString"];
                if (string.IsNullOrWhiteSpace(connectionString)) return "DefaultConnection";
                return connectionString;
            }
        }


        private static string schema;
        public static string Schema
        {
            get
            {
                if (!string.IsNullOrWhiteSpace(schema)) return schema;
                var key = ConfigurationManager.AppSettings.AllKeys.FirstOrDefault(x => x.ToUpper() == "SCHEMA");
                if (!string.IsNullOrEmpty(key)) schema = ConfigurationManager.AppSettings[key];
                if (string.IsNullOrWhiteSpace(schema)) return "dbo";
                schema = schema.ToUpper();
                return schema;
            }
        }

        private static string supperAdmin;
        public static string SUPPER_ADMIN
        {
            get
            {
                if (!string.IsNullOrWhiteSpace(supperAdmin)) return supperAdmin;
                var key = ConfigurationManager.AppSettings.AllKeys.FirstOrDefault(x => x.ToUpper() == "SUPPER_ADMIN");
                if (!string.IsNullOrEmpty(key)) supperAdmin = ConfigurationManager.AppSettings[key];
                if (string.IsNullOrWhiteSpace(supperAdmin)) return "SYSADMIN";
                supperAdmin = supperAdmin.ToUpper();
                return supperAdmin;
            }
        }

        private static string tablePrefix;
        public static string TABLE_PREFIX
        {
            get
            {
                if (!string.IsNullOrWhiteSpace(tablePrefix)) return tablePrefix;
                var key = ConfigurationManager.AppSettings.AllKeys.FirstOrDefault(x => x.ToUpper() == "TABLE_PREFIX");
                if (!string.IsNullOrEmpty(key)) tablePrefix = ConfigurationManager.AppSettings[key];
                if (string.IsNullOrWhiteSpace(tablePrefix)) return string.Empty;
                tablePrefix = tablePrefix.ToUpper();
                return tablePrefix;
            }
        }

        private static ConvertType? namingType;
        public static ConvertType NamingType
        {
            get
            {
                if (namingType.HasValue) return namingType.Value;
                var key = ConfigurationManager.AppSettings.AllKeys.FirstOrDefault(x => x == "NamingType");
                string stringVal = string.Empty;
                if (!string.IsNullOrEmpty(key)) stringVal = ConfigurationManager.AppSettings[key];
                if (string.IsNullOrWhiteSpace(stringVal))
                {
                    namingType = ConvertType.CAPITALIZED;
                    return ConvertType.CAPITALIZED;
                }
                stringVal = stringVal.ToUpper();

                ConvertType result = ConvertType.CAPITALIZED;
                if (Enum.TryParse<ConvertType>(stringVal, out result))
                {
                    namingType = result;
                    return result;
                }

                return result;
            }
        }



        private static bool? isSplitColumn;
        public static bool IS_SPLIT_COLUMN_NAME
        {
            get
            {
                if (isSplitColumn.HasValue) return isSplitColumn.Value;
                var key = ConfigurationManager.AppSettings.AllKeys.FirstOrDefault(x => x.ToUpper() == "IS_SPLIT_COLUMN_NAME");
                string stringVal = string.Empty;
                if (!string.IsNullOrEmpty(key)) stringVal = ConfigurationManager.AppSettings[key];
                if (string.IsNullOrWhiteSpace(stringVal))
                {
                    isSplitColumn = true;
                    return true;
                }

                stringVal = stringVal.Trim().ToUpper();
                if (stringVal == "1" || stringVal == "TRUE")
                {
                    isSplitColumn = true;
                    return true;
                }

                isSplitColumn = false;
                return false;
            }
        }

        private static bool? isSplitTable;
        public static bool IS_SPLIT_TABLE_NAME
        {
            get
            {
                if (isSplitTable.HasValue) return isSplitTable.Value;
                var key = ConfigurationManager.AppSettings.AllKeys.FirstOrDefault(x => x.ToUpper() == "IS_SPLIT_TABLE_NAME");
                string stringVal = string.Empty;
                if (!string.IsNullOrEmpty(key)) stringVal = ConfigurationManager.AppSettings[key];
                if (string.IsNullOrWhiteSpace(stringVal))
                {
                    isSplitTable = true;
                    return true;
                }

                stringVal = stringVal.Trim().ToUpper();
                if (stringVal == "1" || stringVal == "TRUE")
                {
                    isSplitTable = true;
                    return true;
                }

                isSplitTable = false;
                return false;
            }
        }

        private static Guid applicationKey;
        public static Guid ApplicationKey
        {
            get
            {
                if (applicationKey != Guid.Empty) return applicationKey;
                var key = ConfigurationManager.AppSettings.AllKeys.FirstOrDefault(x => x == "ApplicationKey");

                if (!string.IsNullOrEmpty(key) && Guid.TryParse(ConfigurationManager.AppSettings[key], out applicationKey))
                    return applicationKey;

                return Guid.Empty;
            }
        }

        private static int defaultLongCacheTime = 0;
        public static int DefaultLongCacheTime
        {
            get
            {
                if (defaultLongCacheTime > 0) return defaultLongCacheTime;
                if (!int.TryParse(ConfigurationManager.AppSettings["DefaultLongCacheTime"], out defaultLongCacheTime))
                {
                    defaultLongCacheTime = 1440;
                }
                return defaultLongCacheTime;
            }
        }


        private static int defaultCacheTime = 0;
        public static int DefaultCacheTime
        {
            get
            {
                if (defaultCacheTime > 0) return defaultCacheTime;
                if (!int.TryParse(ConfigurationManager.AppSettings["DefaultCacheTime"], out defaultCacheTime))
                {
                    defaultCacheTime = 5;
                }
                return defaultCacheTime;
            }
        }

        private static float smallCacheTime = 0;
        public static float SmallCacheTime
        {
            get
            {
                if (smallCacheTime > 0) return smallCacheTime;
                if (!float.TryParse(ConfigurationManager.AppSettings["SmallCacheTime"], out smallCacheTime))
                {
                    smallCacheTime = 0.2f;
                }
                return smallCacheTime;
            }
        }

        private static string registerGroup;
        public static string RegisterGroup
        {
            get
            {
                if (!string.IsNullOrWhiteSpace(registerGroup)) return registerGroup;
                registerGroup = ConfigurationManager.AppSettings["RegisterGroup"];
                if (string.IsNullOrWhiteSpace(registerGroup))
                {
                    registerGroup = "Register";
                }
                return registerGroup;
            }
        }

        private static string deleteRoleAction;
        public static string DeleteRoleAction
        {
            get
            {
                if (!string.IsNullOrWhiteSpace(deleteRoleAction)) return deleteRoleAction;
                deleteRoleAction = ConfigurationManager.AppSettings["DeleteRoleAction"];
                if (string.IsNullOrWhiteSpace(deleteRoleAction))
                {
                    deleteRoleAction = "/system/delete_role";
                }
                return deleteRoleAction;
            }
        }


        private static string deleteObjectAction;
        public static string DeleteObjectAction
        {
            get
            {
                if (!string.IsNullOrWhiteSpace(deleteObjectAction)) return deleteObjectAction;
                deleteObjectAction = ConfigurationManager.AppSettings["DeleteObjectAction"];
                if (string.IsNullOrWhiteSpace(deleteObjectAction))
                {
                    deleteObjectAction = "/system/delete_object";
                }
                return deleteObjectAction;
            }
        }

        private static string deleteGroupAction;
        public static string DeleteGroupAction
        {
            get
            {
                if (!string.IsNullOrWhiteSpace(deleteGroupAction)) return deleteGroupAction;
                deleteGroupAction = ConfigurationManager.AppSettings["DeleteGroupAction"];
                if (string.IsNullOrWhiteSpace(deleteGroupAction))
                {
                    deleteGroupAction = "/account/delete_group";
                }
                return deleteGroupAction;
            }
        }

        private static string activeUserAction;
        public static string ActiveUserAction
        {
            get
            {
                if (!string.IsNullOrWhiteSpace(activeUserAction)) return activeUserAction;
                activeUserAction = ConfigurationManager.AppSettings["ActiveUserAction"];
                if (string.IsNullOrWhiteSpace(activeUserAction))
                {
                    activeUserAction = "/account/active_user";
                }
                return activeUserAction;
            }
        }

        private static string dateTimeFormat;
        public static string DateTimeFormat
        {
            get
            {
                if (!string.IsNullOrWhiteSpace(dateTimeFormat)) return dateTimeFormat;
                dateTimeFormat = ConfigurationManager.AppSettings["DateTimeFormat"];
                if (string.IsNullOrWhiteSpace(dateTimeFormat))
                {
                    dateTimeFormat = "MM/dd/yyyy";
                }
                return dateTimeFormat;
            }
        }

        public const string ALL_REGION_CODE = "00";
        public const string WT_SN_VL = "WT_SN_VL";
        public const string PURPOSE_TYPE = "W";
    }
}
